package cn.xo68.boot.ui.common;

import cn.xo68.boot.ui.entity.RequireConfig;
import cn.xo68.boot.ui.entity.RequirePathMapping;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * require配置构建者
 * @author wuxie
 * @version 2019-3-20
 */
public class RequireConfigBuilder {

    private String baseUrl=RequireConfig.BASE_URL_DEFAULT;
    private int waitSeconds=5;

    private final List<RequirePathMapping> requirePathMappings;
    private final List<String> depsList;

    public static RequireConfigBuilder getInstance(){
        return new RequireConfigBuilder();
    }

    public RequireConfigBuilder() {
        this.requirePathMappings = new ArrayList<>();
        this.depsList= new LinkedList<>();
        insertDefaultPaths();
        insertDefaultDeps();
    }

    private void insertDefaultPaths(){
        requirePathMappings.add(new RequirePathMapping("text","/ui/lib/require/text"));
        requirePathMappings.add(new RequirePathMapping("css","/ui/lib/require/css.min"));
        requirePathMappings.add(new RequirePathMapping("qs","/ui/lib/qs/qs"));
        requirePathMappings.add(new RequirePathMapping("axios","/ui/lib/axios/axios.min"));
        requirePathMappings.add(new RequirePathMapping("cookie","/ui/lib/cookie/js.cookie.min"));
        requirePathMappings.add(new RequirePathMapping("vue","/ui/lib/vue/vue.min"));
        requirePathMappings.add(new RequirePathMapping("vue-router","/ui/lib/vue/vue-router.min"));
        requirePathMappings.add(new RequirePathMapping("vuex","/ui/lib/vue/vuex.min"));
        requirePathMappings.add(new RequirePathMapping("iview","/ui/lib/iview/iview.min"));
        requirePathMappings.add(new RequirePathMapping("echarts","/ui/lib/echarts/echarts.min"));

        requirePathMappings.add(new RequirePathMapping("WxCore","/ui/common/WxCore"));
        requirePathMappings.add(new RequirePathMapping("resolveComponent","/ui/common/WxCore"));


        requirePathMappings.add(new RequirePathMapping("httpClient","/ui/common/httpClient"));
        //requirePathMappings.add(new RequirePathMapping("restTemplate","/common/restTemplate"));
        requirePathMappings.add(new RequirePathMapping("ChartBar","/ui/component/chart/ChartBar"));
        requirePathMappings.add(new RequirePathMapping("ChartPie","/ui/component/chart/ChartPie"));
    }

    private void insertDefaultDeps(){
        this.depsList.add("text");
        this.depsList.add("css");
        this.depsList.add("qs");
        this.depsList.add("vue");
        this.depsList.add("vue-router");
        this.depsList.add("vuex");
        this.depsList.add("iview");
        this.depsList.add("WxCore");
        this.depsList.add("httpClient");
    }

    public RequireConfigBuilder setBaseUrl(String baseUrl){
        this.baseUrl = baseUrl;
        return this;
    }
    public RequireConfigBuilder setWaitSeconds(int waitSeconds){
        this.waitSeconds=waitSeconds;
        return this;
    }

    public RequireConfigBuilder addPathMapping(RequirePathMapping requirePathMapping){
        this.requirePathMappings.add(requirePathMapping);
        return this;
    }

    public RequireConfigBuilder addDeps(String deps){
        this.depsList.add(deps);
        return this;
    }

    public RequireConfig build(){
        RequireConfig requireConfig = new RequireConfig();
        requireConfig.setBaseUrl(this.baseUrl);
        requireConfig.addPaths(requirePathMappings);
        requireConfig.setWaitSeconds(this.waitSeconds);
        requireConfig.addDepsAll(this.depsList);
        return requireConfig;
    }
}
